package com.bank.serviceimpl;

import com.bank.dao.AccountDao;
import com.bank.daoimpl.AccountDaoImpl;
import com.bank.javabean.Account;
import com.bank.service.AdminService;

import java.util.ArrayList;

/**
 * @Auther: Liu
 */
public class AdminServiceImpl implements AdminService {
    //admin服务需要对数据库操作 创建对应的数据库对象
    private AccountDao accountDao=new AccountDaoImpl();

    @Override
    public Account AdminLogin(String username, String password) {
        ArrayList<Account> accounts = accountDao.selectByUsernameAndPassword(username, password);
        if(accounts.isEmpty()){
            return null;
        }else{
            return accounts.get(0);
        }
    }

    @Override
    public void addAdmin(String username, String password, String name, String mail, String phone) {
        Account account=new Account(username,password,name,mail,phone,1);
        accountDao.insertAccount(account);
    }

    @Override
    public void addUser(String username, String password, String name, String mail, String phone) {
        Account account=new Account(username,password,name,mail,phone,2);
        accountDao.insertAccount(account);
    }

    @Override
    public void removeAccount(int myId, int id) {
        //只能删除非当期账号
        if(myId!=id){
            accountDao.deleteById(id);
        }
    }

    @Override
    public void restPassword(int myId, int id, String password) {
        //只能修改当前账号与其他普通用户
        if(myId==id){//当期账号
            accountDao.updatePasswordById(id,password);
        }else{
            //查询指定id信息 根据type判断
            ArrayList<Account> accounts = accountDao.selectById(id);
            if(accounts.isEmpty()){

            }else{
                Account account = accounts.get(0);
                //判断修改的指定账号是否是普通用户
                if(account.getType()==2){
                    accountDao.updatePasswordById(id,password);
                }
            }

        }
    }

    @Override
    public  ArrayList<Account> findAccount(int type) {
        return accountDao.selectByType(type);
    }

    @Override
    public void forgetPassword(String username, int type) {
        //先根据用户名查询数据
        ArrayList<Account> accounts = accountDao.selectByUsername(username);
        if(accounts.isEmpty()){
            //账号不存在
        }else{
            Account account = accounts.get(0);
            //判断账号类型是否匹配
            if(account.getType()==type){
                accountDao.updatePasswordById(account.getId(),"111111");
            }
        }

    }
}
